import imp


import numpy as np
import scipy.sparse as sp
from scipy.io import savemat
import pandas as pd
from sklearn.preprocessing import normalize


def load_data_xlsx():
    data_vec = pd.read_excel('./vec.xls')
    data_lab = pd.read_excel('./label.xls')
    data_rel = pd.read_excel('./relation.xls')
    data_idx = pd.read_excel('./index.xls')
    map_index = dict(zip(data_idx['i'], range(data_idx.size)))
    node_num = data_vec.shape[0]
    # value = np.ones(data_rel.shape[0])
    # rel = sp.csr_matrix(data_rel)

    attributes = np.array(data_vec)
    attributes = normalize(attributes, axis=0, norm='max')
    attributes = sp.csr_matrix(attributes)
    # print(attributes)
    # attributes = sp.csr_matrix(data_vec)
    labels = np.array(1 - data_lab)
    # row_col = [(x, y) for x, y in zip(data_rel['xf'], data_rel['gf'])]
    row_col = [[map_index[x], map_index[y]] for x, y in zip(data_rel['xf'], data_rel['gf']) if x in map_index.keys() and y in map_index.keys()]
    row = np.array([x[0] for x in row_col])
    col = np.array([x[1] for x in row_col])
    ones = np.ones(len(row_col))
    network = sp.lil_matrix(sp.csc_matrix((ones, (row, col)), shape=(node_num, node_num)))

    file_name = 'ours.mat'
    savemat(file_name, {'Label':labels, 'Attributes':attributes, 'Network':network})
    
    return network, attributes, labels

load_data_xlsx()